<template>
  <div style="padding: 10px">
    <el-tag style="width: 100%; display: flex">
      <i class="el-icon-warning"
         style="margin: 10px 5px 0"></i>
      <div style="color: black">
        此功能根据您配置的值进行检测部分低价引流问题，结果仅供参考，并不能完全匹配抖店检测违规商品条件，建议您严格按照抖店规范发布商品。
      </div>
      <el-link type="primary"
               @click="link">查看抖店规则 </el-link>
    </el-tag>
    <div style="margin-top: 20px; display: flex">
      <div style="display: flex">
        <div style="line-height: 30px; margin-right: 10px">商品状态:</div>
        <div>
          <el-select v-model="shaop_status.value"
                     placeholder="请选择">
            <el-option v-for="item in shaop_status.options"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </div>
      </div>
      <div style="display: flex; margin-left: 20px">
        <div style="line-height: 30px; margin-right: 10px">商品名称:</div>
        <div>
          <el-input v-model="shaop_status.input"
                    placeholder="请选择" />
        </div>
      </div>
      <div style="display: flex; margin-left: 20px">
        <div style="line-height: 30px; margin-right: 10px">创建时间:</div>
        <div>
          <el-date-picker v-model="shaop_status.value1"
                          type="daterange"
                          range-separator="至"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期">
          </el-date-picker>
        </div>
      </div>
      <div style="margin-left: 20px">
        <el-button type="primary"
                   size="mini"
                   @click="start()">开始检测</el-button>
        <el-button type="primary"
                   size="mini"
                   @click="DetectionSettings()">检测设置</el-button>
        <!-- <el-button type="primary" size="mini" @click="DetectionSKU()"
          >批量修改SKU分类</el-button
        > -->
        <el-popover style="margin-left: 10px"
                    placement="bottom"
                    width="150"
                    trigger="hover">
          <div style="display: flex; justify-content: space-between">
            <el-button type="primary"
                       size="mini"
                       @click="btadd()">上架</el-button>
            <el-button type="danger"
                       size="mini"
                       @click="bt3()">下架</el-button>
          </div>
          <el-button type="primary"
                     size="mini"
                     slot="reference">批量上下架</el-button>
        </el-popover>
      </div>
    </div>
    <div style="display: flex; margin-top: 10px; font-size: 14px">
      <div>共检测【{{ alllist.length }}】个商品,</div>
      <div>检测到符合条件的商品【{{ tableData.length }}】个,</div>
      <div>已勾选【{{ handleSelectionChangelist.length }}】,</div>
    </div>
    <div style="margin-top: 20px">
      <el-table v-loading="loading"
                element-loading-text="数据加载中"
                ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                height="700"
                empty-text=" ">
        <el-table-column type="selection"
                         width="55"> </el-table-column>
        <el-table-column type="index"
                         label="序号"> </el-table-column>
        <el-table-column prop=""
                         label="图片"
                         width="120">
          <template slot-scope="scope">
            <div>
              <el-image style="width: 100px; height: 100px"
                        :src="scope.row.img"
                        :preview-src-list="[scope.row.img]">
              </el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop=""
                         label="商品信息"
                         width="400">
          <template slot-scope="scope">
            <div>
              <el-link type="primary"
                       @click="links(scope.row)">
                {{ scope.row.name }}</el-link>
            </div>
            <div>ID:{{ scope.row.product_id }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="category_name"
                         label="类目"
                         width="200">
        </el-table-column>
        <el-table-column prop=""
                         label="状态">
          <template slot-scope="scope">
            <el-tag size="mini">{{ scope.row.tab }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop=""
                         label="检测结果"
                         width="250">
          <!-- result -->
          <template slot-scope="scope">
            <div v-if="scope.row.result">
              <div v-for="(item, index) in scope.row.result"
                   :key="index"
                   style="color: red; font-size: 12px">
                {{ index + 1 }} . {{ item }}
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop=""
                         label="价 格"
                         width="170">
          <template slot-scope="scope">
            {{ (scope.row.price_lower / 100).toFixed(2) }}~
            {{ (scope.row.price_higher / 100).toFixed(2) }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="" label="SKU数量"> </el-table-column> -->
        <el-table-column prop="update_time"
                         label="创建时间"
                         width="150">
        </el-table-column>
        <el-table-column prop="operationres"
                         label="操作结果">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.alerts === '操作成功'"
                    size="mini">
              {{ scope.row.alerts }}</el-tag>
            <el-tag v-if="scope.row.alerts === '已上架'"
                    size="mini">
              {{ scope.row.alerts }}</el-tag>
            <el-tag type="danger"
                    v-if="scope.row.alerts === '已下架'"
                    size="mini">
              {{ scope.row.alerts }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop=""
                         label="操作">
          <template slot-scope="scope">
            <el-button size="mini"
                       type="primary"
                       @click="ModifySKU(scope.row)">
              修改SKU
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 检测设置 -->
    <el-dialog title="低价引流检测设置"
               :visible.sync="centerDialogVisible"
               width="40%">
      <div>
        <div style="display: flex">
          <div style="width: 150px">异常SKU名称引流 ：</div>
          <div style="display: flex">
            <el-checkbox v-model="Detection.checked">SKU名称包含</el-checkbox>
            <el-input type="textarea"
                      v-model="Detection.input"
                      placeholder="多个关键字用逗号隔开"
                      :rows="2"
                      style="margin-top: -10px; margin-left: 20px; width: 400px" />
          </div>
        </div>
        <div style="display: flex; margin-top: 20px">
          <div style="width: 150px; letter-spacing: 4px">商品价格异常 ：</div>
          <div>
            <div>
              <el-checkbox v-model="Detection.check1">商品最低价SKU未超过商品SKU价格均值一半</el-checkbox>
            </div>
            <div style="margin-top: 10px">
              <el-checkbox v-model="Detection.check2.check2">商品最高SKU价格大于最低价
                <el-input style="width: 100px; margin: 0 10px"
                          v-model="Detection.check2.input"
                          type="number"
                          placeholder="请输入倍数"></el-input>倍
              </el-checkbox>
            </div>
            <div style="margin-top: 10px">
              <el-checkbox v-model="Detection.check3.check3">商品最高SKU超过最低价SKU
                <el-input style="width: 100px; margin: 0 10px"
                          v-model="Detection.check3.input"
                          placeholder="请输入金额"
                          type="number"></el-input>元
              </el-checkbox>
            </div>
            <div style="margin-top: 10px">
              <el-checkbox v-model="Detection.check4.check4">商品最高价超过
                <el-input style="width: 100px; margin: 0 10px"
                          v-model="Detection.check4.input"
                          placeholder="请输入金额"
                          type="number"></el-input>元
              </el-checkbox>
            </div>
            <div style="margin-top: 10px">
              <el-checkbox v-model="Detection.check5.check5">商品最低价低于
                <el-input style="width: 100px; margin: 0 10px"
                          v-model="Detection.check5.input"
                          placeholder="请输入金额"
                          type="number"></el-input>元
              </el-checkbox>
            </div>
          </div>
        </div>
      </div>
      <span slot="footer"
            class="dialog-footer">
        <el-button size="mini"
                   type="primary"
                   @click="formbtn()">保存设置</el-button>
        <el-button size="mini"
                   type="danger"
                   @click="centerDialogVisible = false">关闭窗口</el-button>
      </span>
    </el-dialog>
    <!-- SKU分类 -->
    <el-dialog title="批量修改SKU分类"
               :visible.sync="centerDialogVisible2"
               width="35%">
      <div style="height: 400px">
        <div>
          <div style="font-weight: 900; font-size: 20px">选择修改的商品</div>
          <div style="margin: 10px 0">
            <el-radio v-model="SKUs.radio"
                      label="1">勾选的商品【{{ handleSelectionChangelist.length }}】个</el-radio>
            <el-radio v-model="SKUs.radio"
                      label="2">所有商品【{{ tableData.length }}】个</el-radio>
          </div>
        </div>
        <div style="margin-top: 20px; height: 280px">
          <div style="font-weight: 900; font-size: 20px">选择修改的方式</div>
          <div style="margin: 10px 0">
            <div style="display: flex; justify-content: space-between; width: 80%">
              <el-checkbox v-model="SKUs.checked"
                           style="line-height: 30px">统一设置SKU分类</el-checkbox>
              <el-select v-model="SKUs.value1"
                         placeholder="选择SKU分类（必选）"
                         :disabled="!SKUs.checked">
                <el-option v-for="item in SKUs.options1"
                           style="width: 450px"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                  <span style="
                      float: left;
                      margin-top: -6px;
                      font-size: 14px;
                      font-weight: 900;
                    ">{{ item.label }}</span>
                  <span style="
                      float: left;
                      display: block;
                      position: absolute;
                      margin-top: 20px;
                      font-size: 12px;
                    ">{{ item.name }}</span>
                </el-option>
              </el-select>
            </div>
            <div style="
                display: flex;
                justify-content: space-between;
                width: 80%;
                margin-top: 20px;
              ">
              <el-checkbox v-model="SKUs.checked2"
                           style="line-height: 30px">最低价SKU单独设置SKU分类</el-checkbox>
              <el-select v-model="SKUs.value2"
                         placeholder="选择SKU分类（必选）"
                         :disabled="!SKUs.checked2">
                <el-option v-for="item in SKUs.options2"
                           style="width: 450px"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value">
                  <span style="
                      float: left;
                      margin-top: -6px;
                      font-size: 14px;
                      font-weight: 900;
                    ">{{ item.label }}</span>
                  <span style="
                      float: left;
                      display: block;
                      position: absolute;
                      margin-top: 20px;
                      font-size: 12px;
                    ">{{ item.name }}</span>
                </el-option>
              </el-select>
            </div>
            <div style="
                display: flex;
                margin-top: 20px;
                justify-content: space-between;
              ">
              <div>若需要修改的商品存在SKU分类, 修改方式:</div>
              <div style="display: flex">
                <el-radio v-model="SKUs.radio2"
                          label="1">跳过不处理</el-radio>
                <el-radio v-model="SKUs.radio2"
                          label="2">不跳过, 统一按本次修改</el-radio>
              </div>
            </div>
            <div style="margin-top: 30px">
              <div>温馨提示：</div>
              <div>
                1.
                准确对SKU进行分类，有助获得更多流量和曝光，有误及未填写可能会影响商品的曝光机会
              </div>
              <div>2. 若类目不支持设置SKU分类，则批量修改无效</div>
              <div>
                3. 若所有SKU价格都一样，批量修改有勾选" 统一设置SKU分类
                "，则按所有SKU，统一设置为准
              </div>
            </div>
          </div>
          <span style="margin: 10px 0">
            <div slot="footer"
                 style="float: right">
              <el-button size="mini"
                         type="primary"
                         @click="allset()">确认修改</el-button>
              <el-button size="mini"
                         type="danger"
                         @click="centerDialogVisible2 = false">取消修改</el-button>
            </div>
          </span>
        </div>
      </div>
    </el-dialog>
    <!-- 修改SKU -->
    <el-dialog title="修改SKU"
               :visible.sync="dialogVisiblesku"
               width="50%">
      <div v-loading="loads">
        <div style="display: flex">
          <div style="margin: 0 10px 0 0">
            <el-image style="width: 100px; height: 100px"
                      :src="this.skudata.img"
                      :preview-src-list="[this.skudata.img]">
            </el-image>
          </div>
          <div>
            <el-link type="primary"
                     @click="links(skudata)">{{
              skudata.name
            }}</el-link><br />
            ID:{{ skudata.product_id }}<br />
            <div v-if="skudata.category_detail">
              {{ skudata.category_detail.first_cname }}>{{
                skudata.category_detail.second_cname
              }}>{{ skudata.category_detail.third_cname }}
            </div>
          </div>
        </div>
        <div style="
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
          ">
          <div>
            <el-tag type="primary"
                    style="margin-right: 10px">共【{{ tableDatas.length }}】个SKU, 已勾选【{{
                handleSelectionChangeslist.length
              }}】个</el-tag>
            <el-popover placement="top"
                        title="批量改价格"
                        width="300"
                        trigger="click">
              <div>
                <div style="display: flex">
                  <div style="margin-top: 4px">原价*</div>
                  <el-input type="number"
                            style="width: 100px"
                            v-model="diasku.input1"></el-input>+<el-input type="number"
                            v-model="diasku.input2"
                            style="width: 100px"></el-input>
                </div>
                <div style="float: right; margin-top: 10px; display: flex">
                  <div v-if="this.handleSelectionChangeslist.length == 0"
                       style="margin-top: 5px; margin-right: 5px; color: red">
                    至少勾选 1 个SKU
                  </div>
                  <el-button type="primary"
                             size="mini"
                             :disabled="this.handleSelectionChangeslist.length == 0"
                             @click="skunum()">确定</el-button>
                  <el-button type="danger"
                             size="mini">取消</el-button>
                </div>
              </div>
              <el-button type="primary"
                         size="mini"
                         slot="reference"
                         style="margin-right: 10px">批量改价格</el-button>
            </el-popover>
            <el-popover placement="top"
                        title="批量改分类"
                        width="300"
                        trigger="click">
              <div>
                <div style="display: flex">
                  <el-select v-model="diasku.options"
                             placeholder="选择SKU分类（必选）"
                             :disabled="!SKUs.checked">
                    <el-option v-for="item in SKUs.options1"
                               style="width: 450px"
                               :key="item.value"
                               :label="item.label"
                               :value="item.value">
                      <span style="
                          float: left;
                          margin-top: -6px;
                          font-size: 14px;
                          font-weight: 900;
                        ">{{ item.label }}</span>
                      <span style="
                          float: left;
                          display: block;
                          position: absolute;
                          margin-top: 20px;
                          font-size: 12px;
                        ">{{ item.name }}</span></el-option></el-select>
                </div>
                <div style="float: right; margin-top: 10px; display: flex">
                  <div v-if="this.handleSelectionChangeslist.length == 0"
                       style="margin-top: 5px; margin-right: 5px; color: red">
                    至少勾选 1 个SKU
                  </div>
                  <el-button type="primary"
                             size="mini"
                             :disabled="this.handleSelectionChangeslist.length == 0">确定</el-button>
                  <el-button type="danger"
                             size="mini">取消</el-button>
                </div>
              </div>
              <!-- <el-button
                type="primary"
                size="mini"
                slot="reference"
                style="margin-right: 10px"
                >批量改分类</el-button
              > -->
            </el-popover>
            <el-popover placement="top"
                        title="批量改状态"
                        width="300"
                        trigger="click">
              <div>
                <div style="display: flex">
                  <el-switch v-model="diasku.delivery"></el-switch>
                </div>
                <div style="float: right; margin-top: 10px; display: flex">
                  <div v-if="this.handleSelectionChangeslist.length == 0"
                       style="margin-top: 5px; margin-right: 5px; color: red">
                    至少勾选 1 个SKU
                  </div>
                  <el-button type="primary"
                             size="mini"
                             :disabled="this.handleSelectionChangeslist.length == 0"
                             @click="skustatus()">确定</el-button>
                  <el-button type="danger"
                             size="mini">取消</el-button>
                </div>
              </div>
              <el-button type="primary"
                         size="mini"
                         slot="reference"
                         style="margin-right: 10px">批量改状态</el-button>
            </el-popover>
          </div>
          <div>
            <el-button type="primary"
                       size="mini"
                       @click="links(skudata)">前往抖店后台修改</el-button>
          </div>
        </div>
        <el-table :data="tableDatas"
                  ref="multipleTable"
                  tooltip-effect="dark"
                  style="width: 100%"
                  height="400"
                  @selection-change="handleSelectionChanges">
          <el-table-column type="selection"
                           width="55"> </el-table-column>
          <el-table-column type="index"
                           label="序号"> </el-table-column>
          <el-table-column prop="sell_properties[0].value_name"
                           :label="aname"
                           width="200">
          </el-table-column>
          <el-table-column prop=" "
                           label="价 格">
            <template slot-scope="scope">
              <!-- {{ (scope.row.price / 100).toFixed(2) }} -->
              <el-input v-model="scope.row.price"
                        type="number"
                        style="width: 100px"></el-input>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="input" label="改后价格"></el-table-column>
          <el-table-column prop="" label="改后价格">
            <template v-slot="scope">
              <div>
                {{ scope.row.input }}
                <input
                  v-model="scope.row.input"
                  type="number"
                  style="width: 100px"
                />
              </div>
            </template>
          </el-table-column> -->
          <el-table-column prop="self_sell_stock_num"
                           label="库存">
          </el-table-column>
          <!-- <el-table-column prop="" label="sku分类">
            <template slot-scope="scope">
              <div>
                <el-select
                  v-model="scope.row.classification"
                  placeholder="选择SKU分类（必选）"
                  :disabled="!SKUs.checked"
                >
                  <el-option
                    v-for="item in SKUs.options1"
                    style="width: 450px"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                    <span
                      style="
                        float: left;
                        margin-top: -6px;
                        font-size: 14px;
                        font-weight: 900;
                      "
                      >{{ item.label }}</span
                    >
                    <span
                      style="
                        float: left;
                        display: block;
                        position: absolute;
                        margin-top: 20px;
                        font-size: 12px;
                      "
                      >{{ item.name }}</span
                    ></el-option
                  ></el-select
                >
              </div>
            </template>
          </el-table-column> -->
          <el-table-column prop=""
                           label="sku状态"
                           width="150">
            <template slot-scope="scope">
              <div style="display: flex">
                <div v-if="scope.row.sku_status">已上架</div>
                <div v-else>已下架</div>
                <el-switch v-model="scope.row.sku_status"
                           style="margin: 0 0 0 10px"></el-switch>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <span slot="footer"
            class="dialog-footer">
        <el-button type="primary"
                   size="mini"
                   @click="firmsku()">确认修改</el-button>
        <el-button type="danger"
                   size="mini"
                   @click="dialogVisiblesku = false">取消修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  previewDetail,
  tproduct,
  getShopIdData,
  launchProduct,
  skulist,
  index,
  canOnline,
  batchOffline,
} from '@/api/shopsApi'
export default {
  data() {
    return {
      loads: true,
      aname: '',
      handleSelectionChangeslist: [],
      diasku: {
        input1: '1',
        input2: '',
        options: '',
        delivery: false,
      },
      classification: '',
      tableDatas: [],
      skudata: {},
      dialogVisiblesku: false,
      Detection: {
        check1: false,
        checked: false,
        input: '',
        check2: {
          check2: '',
          input: '1',
        },
        check3: {
          check3: '',
          input: '',
        },
        check4: {
          check4: '',
          input: '',
        },
        check5: {
          check5x: '',
          input: '',
        },
      },
      SKUs: {
        radio: '1',
        checked: true,
        options1: [
          {
            value: '1',
            label: '主售款（单品）',
            name: 'sku为单个商品，与标题，主图，叶子类目描述一致，为该商品主要售卖款式',
          },
          {
            value: '2',
            label: '主售款（多件装）',
            name: 'sku为主售款，（单品）的多件装',
          },
          {
            value: '3',
            label: '主售款（组合装）',
            name: 'sku为主售款，（单品）+配件或者搭配',
          },
          {
            value: '4',
            label: '非主售款',
            name: 'sku不是该商品的主要销售款式，在材质/款式/数量/使用功能上与主售款有明星差别',
          },
          {
            value: '5',
            label: '信息说明/防拍',
            name: 'sku非实际可售卖的商品，仅为信息的补充说明，或为高价防拍',
          },
        ],
        value1: '',
        checked2: false,
        options2: [
          {
            value: '1',
            label: '主售款（单品）',
            name: 'sku为单个商品，与标题，主图，叶子类目描述一致，为该商品主要售卖款式',
          },
          {
            value: '2',
            label: '主售款（多件装）',
            name: 'sku为主售款，（单品）的多件装',
          },
          {
            value: '3',
            label: '主售款（组合装）',
            name: 'sku为主售款，（单品）+配件或者搭配',
          },
          {
            value: '4',
            label: '非主售款',
            name: 'sku不是该商品的主要销售款式，在材质/款式/数量/使用功能上与主售款有明星差别',
          },
          {
            value: '5',
            label: '信息说明/防拍',
            name: 'sku非实际可售卖的商品，仅为信息的补充说明，或为高价防拍',
          },
        ],
        value2: '',
        radio2: '1',
      },
      loading: false,
      centerDialogVisible: false,
      centerDialogVisible2: false,
      tableData: [],
      // 选项
      shaop_status: {
        options: [
          {
            value: '2',
            label: '售卖中',
          },
          {
            value: '1',
            label: '已下架',
          },
        ],
        value: '2',
        input: '',
        value1: '',
      },
      starttime: '',
      endtime: '',
      alllist: [],
      SKUlist: [],
      handleSelectionChangelist: [],
    }
  },
  mounted() {
    // this.loading = true;
    // setTimeout(() => {
    //   this.getList();
    // }, 1000);
    // setInterval(() => {
    //   this.$message.success("");
    // }, 1000);
  },
  methods: {
    firmsku() {
      this.tableData.map((item) => {
        if (item.product_id == this.skudata.product_id) {
          item.alerts = '操作成功'
          this.$message.success(item.alerts)
        }
      })
      var a = []
      this.tableDatas.map((item) => {
        a.push({
          price: item.price,
          id: item.id,
          status: item.sku_status,
        })
      })
      this.dialogVisiblesku = false
    },
    skustatus() {
      console.log(this.diasku.delivery)
      this.tableDatas.forEach((item) => {
        console.log(item.sku_status)
        this.handleSelectionChangeslist.forEach((i) => {
          if (item.id == i.id) {
            item.sku_status = this.diasku.delivery
          }
        })
      })
    },
    skunum() {
      if (this.diasku.input2.length > 0 && this.diasku.input1.length > 0) {
        this.tableDatas.forEach((item) => {
          this.handleSelectionChangeslist.forEach((i) => {
            if (item.id == i.id) {
              item.price = (
                (item.input * Number(this.diasku.input1) +
                  Number(this.diasku.input2 + '00')) /
                100
              ).toFixed(2)
              return
            }
          })
        })
      } else {
        this.$message.error('请输入正确的数值')
      }
    },
    btadd() {
      if (this.handleSelectionChangelist.length > 0) {
        var list = []
        this.handleSelectionChangelist.map((item) => {
          if (item.alerts == '已上架') {
          } else {
            list.push(item)
          }
        })
        if (list.length > 0) {
          const promises = list.map((item) => {
            // console.log(";;", item.product_id);
            return canOnline({
              product_id: item.product_id,
              // shop_id: localStorage.getItem("shop_id"),
            })
          })
          Promise.all(promises)
            .then((results) => {
              // console.log("All requests completed with results:", results);
            })
            .catch((error) => {
              console.error(
                'An error occurred during one or more requests:',
                error
              )
              // 如果有任何请求失败，处理错误
            })
          // }
          this.tableData.map((item) => {
            this.handleSelectionChangelist.map((i) => {
              if (item.product_id == i.product_id) {
                item.alerts = '已上架'
              }
            })
          })
        }
        this.$message.success('操作成功')
        // this.getList();
      } else {
        this.$message.error('请选择数据')
      }
    },
    bt3() {
      if (this.handleSelectionChangelist.length > 0) {
        var list = []
        this.handleSelectionChangelist.map((item) => {
          if (item.status == 1 || item.alerts == '已下架') {
          } else {
            list.push(item)
          }
        })
        console.log(list)
        if (list.length > 0) {
          const promises = list.map((item) => {
            // console.log(";;", item.product_id);
            return batchOffline({
              product_id: item.product_id,
              // shop_id: localStorage.getItem("shop_id"),
            })
          })
          Promise.all(promises)
            .then((results) => {})
            .catch((error) => {
              console.error(
                'An error occurred during one or more requests:',
                error
              )
              // 如果有任何请求失败，处理错误
            })
        }
        this.tableData.map((item) => {
          this.handleSelectionChangelist.map((i) => {
            if (item.product_id == i.product_id) {
              item.alerts = '已下架'
            }
          })
        })
        this.$message.success('操作成功')
      } else {
        this.$message.error('请选择数据')
      }
    },
    allset() {
      this.centerDialogVisible2 = false
    },
    ModifySKU(scope) {
      this.dialogVisiblesku = true
      this.loads = true
      var data = {
        need_conv_pkg: 'false',
        product_id: scope.product_id,
        use_std_brand: 'true',
        appid: '1',
      }
      previewDetail(data).then((res) => {
        console.log('??', res)
        this.skudata = res.data
        this.aname = res.data.display_spec.displaySpecList[0].name
        res.data.spec_prices.map((item, index) => {
          item.input = item.price
          item.price = (item.input / 100).toFixed(2)
          item.classification = ''
        })
        this.tableDatas = res.data.spec_prices
        console.log(this.tableDatas)
        this.loads = false
      })
    },
    links(row) {
      window.open(
        `https://fxg.jinritemai.com/ffa/g/create?product_id=${row.product_id}`
      )
    },
    formbtn() {
      this.SKUlist = this.Detection.input.split(/[, ，]/)
      this.centerDialogVisible = false
      this.$message({
        message: '检测配置项保存成功',
        type: 'success',
      })
    },
    start() {
      this.loading = true
      window.postMessage(
        {
          method: 'wkyygetHttp',
          body: { url: 'http://doulingyang.cn/api' },
        },
        '*'
      )
      let aaa = false
      window.addEventListener('message', function (e) {
        if (e.data.method == 'wkyysetHttp') {
          console.log('eeeee', e)
          aaa = true
        }
      })
      setTimeout((_) => {
        if (aaa) {
          index()
            .then((res) => {
              console.log('res:', res)
              this.getList()
            })
            .catch((error) => {
              console.log('error', error)
              if (error.code == 10008) {
                this.$message.error('请先登录抖店后台')
              }
              this.loading = false
            })
          clearTimeout(_)
        } else {
          this.loading = false
          this.$confirm('使用该功能需要安装插件', '提示', {
            confirmButtonText: '安装插件',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then((res) => {
              window.open('https://doulingyang.cn/')
            })
            .catch((err) => {})
          return
        }
      }, 2000)
    },
    getList() {
      this.loading = true
      var data1 = {
        id_name_code: this.shaop_status.input,
        order_field: 'offline_time',
        business_type: '4',
        not_for_sale_search_type: '1',
        is_offline: '1',
        sort: 'desc',
        appid: '1',
      }
      var data2 = {
        id_name_code: this.shaop_status.input,
        draft_status: '0',
        business_type: '4',
        is_online: '1',
        not_for_sale_search_type: '1',
        check_status: '3',
        status: '0',
        order_field: 'audit_time',
        sort: 'desc',
        appid: '1',
      }
      var data = {}
      if (this.shaop_status.value == '2') {
        data = data2
      } else {
        data = data1
      }
      tproduct({
        ...data,
        page: '0',
        pageSize: '20',
      }).then((res) => {
        var size = Math.ceil(res.total / 20)
        var z = []
        for (var i = 0; i < size; i++) {
          z.push(i)
        }
        const promises = z.map((item) => {
          return tproduct({
            ...data,
            page: item,
            pageSize: '20',
          })
        })
        Promise.all(promises).then((res) => {
          var reslist = []
          this.alllist = []
          this.tableData = []
          res.map((item) => {
            if (item.data) {
              item.data.forEach((items) => {
                reslist.push(items)
                this.alllist.push(items)
              })
            }
          })
          reslist.map((item) => {
            item.result = []
            item.operationres = ''
          })
          function stringToDatestamp(dateString) {
            const date = new Date(dateString)
            return date.getTime()
          }
          // 日期
          if (this.shaop_status.value1) {
            reslist.map((item, index) => {
              // console.log(stringToDatestamp(item.update_time));
              reslist = reslist.filter(
                (item) =>
                  this.convertDateStringToTimestamp(
                    this.shaop_status.value1[0]
                  ) < stringToDatestamp(item.update_time) &&
                  stringToDatestamp(item.update_time) <
                    this.convertDateStringToTimestamp(
                      this.shaop_status.value1[1]
                    )
              )
            })
          }
          // 商品最低价SKU未超过商品SKU价格均值一半
          if (this.Detection.check1) {
            reslist = reslist.filter(
              (item) =>
                (item.price_lower + item.price_higher) / 2 > item.price_lower
            )
            if (reslist) {
              reslist.forEach((item, index) => {
                item.result.push(`商品最低价SKU未超过商品SKU价格均值一半`)
              })
            }
          }
          // 异常SKU名称引流
          if (this.Detection.checked) {
            this.SKUlist.map((item, index) => {
              reslist = reslist.filter((items) =>
                items.category_name.includes(item)
              )
            })
          }
          // 商品最高SKU价格大于最低价
          if (
            this.Detection.check2.check2 &&
            this.Detection.check2.input.length > 0
          ) {
            reslist = reslist.filter(
              (item) =>
                item.price_lower * this.Detection.check2.input <
                item.price_higher
            )
            if (reslist) {
              reslist.forEach((item, index) => {
                item.result.push(
                  `最高SKU价（${(item.price_higher / 100).toFixed(
                    2
                  )}）大于最低价（${(item.price_lower / 100).toFixed(2)}）的【${
                    this.Detection.check2.input
                  }】倍`
                )
              })
            }
          }
          // 商品最高SKU超过最低价SKU
          if (
            this.Detection.check3.check3 &&
            this.Detection.check3.input.length > 0
          ) {
            reslist = reslist.filter(
              (item) =>
                item.price_higher - item.price_lower >=
                Number(this.Detection.check3.input + '00')
            )
            if (reslist) {
              reslist.forEach((item, index) => {
                item.result.push(
                  `最高SKU价(${item.price_higher / 100})超过最低价(${
                    item.price_lower / 100
                  })【${this.Detection.check3.input}】元`
                )
              })
            }
          }
          // 商品最高价超过
          if (
            this.Detection.check4.check4 &&
            this.Detection.check4.input.length > 0
          ) {
            // Detection.check4.input
            reslist = reslist.filter(
              (item) =>
                item.price_higher / 100 >= Number(this.Detection.check4.input)
            )
            if (reslist) {
              reslist.forEach((item, index) => {
                item.result.push(
                  `最高SKU价(${item.price_higher / 100})超过【${
                    this.Detection.check4.input
                  }】元`
                )
              })
            }
          }
          // 商品最低价低于
          if (
            this.Detection.check5.check5 &&
            this.Detection.check5.input.length > 0
          ) {
            reslist = reslist.filter(
              (item) => item.price_higher / 100 < this.Detection.check5.input
            )
            if (reslist) {
              reslist.forEach((item, index) => {
                item.result.push(
                  `最低SKU价（${(item.price_higher / 100).toFixed(2)}）低于【${
                    this.Detection.check5.input
                  }】元`
                )
              })
            }
            // console.log("res", item.price_higher / 100);
          }
          reslist.map((item, index) => {
            if (this.shaop_status.value == 2) {
              item.shaop_status = '售卖中'
            } else {
              item.shaop_status = '已下架'
            }
          })
          // true or false
          if (
            this.Detection.checked ||
            this.Detection.check1 ||
            this.Detection.check2.check2 ||
            this.Detection.check3.check3 ||
            this.Detection.check4.check4 ||
            this.Detection.check5.check5
          ) {
            console.log(reslist, 'reslistreslist')
            this.tableData = reslist
          } else {
            // this.$message.error("");
            this.tableData = []
          }
          this.loading = false
        })
      })
    },
    link() {
      window.open(
        'https://school.jinritemai.com/doudian/web/article/aHztUmxx78fx?rank=1'
      )
    },
    DetectionSettings() {
      this.starttime = this.convertDateStringToTimestamp(
        this.shaop_status.value1[0]
      )
      console.log(this.starttime)
      this.endtime = this.convertDateStringToTimestamp(
        this.shaop_status.value1[1]
      )
      console.log(this.endtime)
      this.centerDialogVisible = true
    },
    convertDateStringToTimestamp(dateString) {
      const date = new Date(dateString)
      return date.getTime()
    },
    DetectionSKU() {
      this.centerDialogVisible2 = true
    },
    handleSelectionChange(val) {
      console.log(val)
      this.handleSelectionChangelist = val
    },
    handleSelectionChanges(val) {
      this.handleSelectionChangeslist = val
      console.log(this.handleSelectionChangeslist)
    },
  },
}
</script>
<style>
.el-input__inner {
  height: 30px;
}
.el-date-editor .el-range__icon {
  line-height: 25px;
}
.el-date-editor .el-range-input,
.el-date-editor .el-range-separator {
  height: 30px;
}
.el-checkbox__input.is-checked + .el-checkbox__label {
  color: black;
}
.el-input__icon {
  height: 100%;
}
.el-select-dropdown__item {
  height: 50px;
}
.el-date-editor .el-range-input {
  border-top: 1px solid rgb(221, 221, 221);
  border-bottom: 1px solid rgb(221, 221, 221);
}
</style>
